package com.guoli.print;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.comm.CommPortIdentifier;
import javax.comm.NoSuchPortException;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;

@SuppressWarnings("unused")
public class TestPrintB {
	
	public static void main(String[] args) {
		String PortName = "";
		//超时等待时间
		int waittime = 0;
		try {
			CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(PortName);
			SerialPort  sPort = (SerialPort) portId.open("串口所有者名称", waittime);
			//用于对串口写数据
			OutputStream os = new BufferedOutputStream(sPort.getOutputStream());
			os.write(new String("data").getBytes());
			//用于从串口读数据
			InputStream is = new BufferedInputStream(sPort.getInputStream());
			int receivedData = is.read();
		} catch (PortInUseException e) {
//			throw new SerialConnectionException(e.getMessage());
			e.printStackTrace();
		} catch (NoSuchPortException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
